package com.atguigu06.project.team.service;

import com.atguigu06.project.team.domain.*;

import static com.atguigu06.project.team.service.Data.*;

/**
 * @author NoobMaster69  * @date 2024/6/2 0002 星期日 22:13
 */
public class NameListService {
    private Employee[] employees;

    public NameListService() {
        employees = new Employee[EMPLOYEES.length];

        for (int i = 0; i < employees.length; i++) {
            int type = Integer.parseInt(EMPLOYEES[i][0]);

            int id = Integer.parseInt(EMPLOYEES[i][1]);
            String name = EMPLOYEES[i][2];
            int age = Integer.parseInt(EMPLOYEES[i][3]);
            double salary = Double.parseDouble(EMPLOYEES[i][4]);
            Equipment equipment;
            double bonus;
            int stock;
            switch (type){
                case EMPLOYEE:
                    employees[i] = new Employee(id,name,age,salary);
                    break;
                case PROGRAMMER:
                    equipment = createEquipment(i);
                    employees[i] = new Programmer(id,name,age,salary,equipment);
                    break;
                case DESIGNER:
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    employees[i] = new Designer(id,name,age,salary,equipment,bonus);
                    break;
                case ARCHITECT:
                    equipment = createEquipment(i);
                    bonus = Double.parseDouble(EMPLOYEES[i][5]);
                    stock = Integer.parseInt(EMPLOYEES[i][6]);
                    employees[i] = new Architect(id,name,age,salary,equipment,bonus,stock);
                    break;
            }
        }
    }

    private Equipment createEquipment(int index){
        int equipmentType = Integer.parseInt(EMPLOYEES[index][0]);

        String modelOrName = EQIPMENTS[index][1];
        String priceOrDisplayOrType = EQIPMENTS[index][2];

        switch (equipmentType){
            case PC:

                return new PC(modelOrName,priceOrDisplayOrType);
            case NOTEBOOK:
                double price = Double.parseDouble(priceOrDisplayOrType);
                return new NoteBook(modelOrName,price);
            case PRINTER:

                return new Printer(modelOrName,priceOrDisplayOrType);
        }
        return null;
    }

    public Employee[] getAllEmployees(){

        return employees;
    }

    public Employee getEmployee(int id) throws TeamException{
        for (int i = 0;i <employees.length;i++){
            if(id == employees[i].getId()){
                return employees[i];
            }
        }
        throw new TeamException("找不到员工信息");
    }

}
